﻿﻿//------------------------------------------------------------------------------
// <copyright file="Common.cs" company="Microsoft">
//
//      Copyright 2013 Microsoft Corporation 
//
//      Licensed under the Microsoft Public License (Ms-PL). 
//      You may not use this file except in compliance with the License.
//
//      Unless required by applicable law or agreed to in writing, software 
//      distributed under the License is distributed on an "AS IS" BASIS,
//      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
//      See the License for the specific language governing permissions and 
//      limitations under the License. 
//
// </copyright>
﻿//------------------------------------------------------------------------------

namespace MyOpenXmlFill
{
    using System;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Spreadsheet;

    public static class MyFill
    {
        public static Fill CreateSolidFillFromRgbString(string hexRgbString)
        {
            if (String.IsNullOrWhiteSpace(hexRgbString) || hexRgbString.Length > 8)
            {
                throw new ArgumentOutOfRangeException("hexRgbString");
            }

            if (hexRgbString.Length < 8)
            {
                hexRgbString = new string('0', 8 - hexRgbString.Length) + hexRgbString;
            }
            
            var hexValue = HexBinaryValue.FromString(hexRgbString);
            return new Fill
            {
                PatternFill = new PatternFill
                {
                    PatternType = PatternValues.Solid,
                    ForegroundColor = new ForegroundColor
                    {
                        Rgb = hexValue
                    }
                }
            };
        }
    }
}
